package team.zero.lemon.module.workflow.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import team.zero.lemon.common.authz.annotation.AuthPassport;
import team.zero.lemon.common.exception.CustomException;
import team.zero.lemon.common.persistence.Page;
import team.zero.lemon.common.web.BaseController;
import team.zero.lemon.module.workflow.entity.Template;
import team.zero.lemon.module.workflow.service.TemplateService;

import java.util.List;


/**
 * Created by bolin on 2016/10/19.
 */


@Controller
@ResponseBody
@AuthPassport
@RequestMapping(value = "/workflow", method = {RequestMethod.POST, RequestMethod.GET})
public class TemplateController extends BaseController {
    @Autowired
    private TemplateService templateService;

    @AuthPassport
    @RequestMapping("/template/publish")
    public boolean publishTemplate(@RequestBody Template template) {
        boolean result = templateService.publish(template);
        return result;
    }


    @AuthPassport
    @RequestMapping("/template/save")
    public Integer saveTemplate(@RequestBody Template template) {
        try {
            Integer id;
            if (template.isInsert())
                id = templateService.save(template);
            else id = template.getId();
            template.setVersionId(id);
            template.setVersionNum(0);
            template.setInsert(false);
            return templateService.save(template);
        } catch (Exception ex) {
            throw new CustomException(ex);
        }

    }

    @AuthPassport
    @RequestMapping("/template/query/id")
    public Template findTemplateById(@RequestBody Template template) {
        Template result = templateService.findTemplateById(template.getId());
        return result;
    }

    @AuthPassport
    @RequestMapping("/template/query/version/all")
    public List<Template> findAllVersionTemplate(@RequestBody Template template) {
        List<Template> result = templateService.findAllVersionTemplate();
        return result;
    }

    @AuthPassport
    @RequestMapping("/template/query/all")
    public Page<Template> findVersionTemplatePage(@RequestBody Template template) {
        Page<Template> result = templateService.findVersionTemplatePage(template);
        return result;
    }
}
